1. Field of the Invention
This invention is related to the field of processors and systems that implement processors, and more particularly to managing locks in systems that include processors.
2. Description of the Related Art
In systems in which multiple threads can be concurrently active, the threads can access the same resource (such as a memory location, a data structure in memory, a device in the system, etc.). To ensure that access to a shared resource is performed in a logically controlled fashion, the shared resource can be protected by a lock. The locks can be used in multiprocessor systems, for example, to prevent overlapping access to the resource by threads executing on different processors. In a multi-threaded system, even if there is only one processor, overlapping access can occur among the multiple active threads if one thread is scheduled while another thread is in the process of accessing/updating the resource.
In order to access the resource or update the resource when a lock is protecting the resource, an accessing thread can acquire the lock, perform the desired accesses/updates, and release the lock. Other threads cannot acquire the lock while the current thread holds the lock. For some locks (generally referred to as “spin locks”), the threads that cannot acquire the lock enter a loop to continuously access the lock until a release is detected (often referred to as “spinning on the lock”). The loop can be very tight, typically including just a few instructions to read the lock and test it for the ability to be acquired. Accordingly, the loop and the corresponding data accesses tend to hit in the caches of the processor. The processor may thus burn significant power waiting for the spin lock to be released, while essentially not accomplishing useful work. Another type of lock is referred to as a “mutex lock.” With a mutex lock, a thread attempts to acquire the lock and, if the attempt fails, the thread blocks so that the processor can do other work while the thread waits for the lock to be released.
In many cases, particularly in multi-processor systems, a thread frequently holds a mutex lock for a fairly short time period. The period of time can be less than the amount of time needed to schedule another thread in place of the blocked thread that is attempting to acquire the lock, for example. Accordingly, an optimization for mutex locks is to enter a spin loop on the lock for an amount of time approximately equal to the typical holding period, or perhaps a little longer. If the lock is released while the thread is still in the spin loop, it may acquire the lock and avoid the overhead of descheduling the thread and rescheduling the thread at a later point. While the thread is in the spin loop, the processor may again consume significant power.
Another mechanism implemented in some systems includes the use of a wait for event instruction and a send event instruction. The wait for event instruction can be used to cause a processor executing a thread to enter a low power state. The processor waits in the low power state for the execution of a send event instruction in another processor (e.g. the processor that is holding the lock), which transmits a send event message. In response to receiving the send event message, the processor returns to the higher power state to attempt to acquire the lock.